package com.zhp.farmerclient.controller;

import com.zhp.farmerclient.common.Result;
import com.zhp.farmerclient.entity.Admin;
import com.zhp.farmerclient.entity.User;
import com.zhp.farmerclient.service.AdminService;
import com.zhp.farmerclient.utils.JwtUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping("/admin")
@CrossOrigin//解决前端后端跨域问题
public class AdminController {
    @Autowired
    public AdminService adminService;

    @Autowired
    public JwtUtils jwtUtils;

    //注册
    @PostMapping("/register")
    public Result register(@RequestBody Admin admin) {
        return adminService.register(admin);
    }
    //登录
    @GetMapping("/login")
    public Result login(@RequestParam String name, @RequestParam String password, HttpServletResponse response){
        return adminService.login(name,password,response);
    }
    // 退出
//    @GetMapping("/logout")
//    @RequiresAuthentication
//    public Result logout() {
//        SecurityUtils.getSubject().logout();
//        return Result.succ();
//    }
    //根据id查看个人信息
    @GetMapping("/check/{id}")
    public Result check(@PathVariable("id") int id){
        return adminService.check(id);
    }
    //修改个人信息
    @PostMapping("/insert")
    public Result save(@Validated @RequestBody Admin admin){
        return adminService.save(admin);
    }
}
